package com.zhida.jiyangcenter.account

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.lifecycle.lifecycleScope
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.utils.*
import chooongg.http.APIException
import chooongg.http.HttpCallback
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.WebNoTokenActivity
import com.zhida.jiyangcenter.api.APIAccount
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.NetResponse
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.mian.MainActivity
import com.zhida.jiyangcenter.utils.REGEX_PASSWORD
import com.zhida.jiyangcenter.utils.SP
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.activity_set_pass.*

@TitleBar(false)
@ContentLayout(R.layout.activity_set_pass)
class SetPassActivity : BaseActivity() {
    override fun initConfig(savedInstanceState: Bundle?) {
        title_bar.navigationIcon(R.drawable.iv_back_fill) {
            finish()
        }
        if (intent.getStringExtra("type")=="account"){
            tv_title.text="请设置登录密码"
        }else{
            title_bar.operationText("跳过") {
//                if (!SP.ISYinsi.get()) {
//                    startActivity<WebNoTokenActivity> {
//                        putExtra(WebNoTokenActivity.URL, "http://beauty.hebzd.com/static/xieyi/privacy.html")
//                        putExtra(WebNoTokenActivity.TITLE, "隐私协议")
//                    }
//                } else {
                    startActivity<MainActivity> { }
//                }
                finish()
            }
        }
        eventConfig()
    }

    private fun eventConfig() {
        switch_password.setOnClickListener {
            if (edit_password.transformationMethod == PasswordTransformationMethod.getInstance()) {
                switch_password.setImageResource(R.drawable.iv_login_visible)
                edit_password.transformationMethod = HideReturnsTransformationMethod.getInstance()
            } else {
                switch_password.setImageResource(R.drawable.iv_login_invisible)
                edit_password.transformationMethod = PasswordTransformationMethod.getInstance()
            }
        }
        btn_login.setOnClickListener {
            when {
                edit_password.text.toString()
                    .isEmpty() -> showSoftInput(edit_password).apply { tipWarn(R.string.login_password_empty) }
                edit_password.text.toString().length < 8 -> showSoftInput(edit_password).apply {
                    tipWarn(
                        "密码最少位8位"
                    )
                }
                !edit_password.text.toString()
                    .regex(REGEX_PASSWORD) -> showSoftInput(edit_password).apply { tipWarn(R.string.login_password_wrong) }
                else -> {
                    lifecycleScope.request({
                        APIAccount.service().passwordAdd(
                            edit_password.text.toString(), edit_password.text.toString()
                        )
                    },
                        object : BaseAPI<Any>() {
                            override fun onError(e: APIException) {
                                tipFail(e.eMessage)
                            }

                            override fun onSuccess(t: Any?, state: Int, msg: String) {
                                tipSuccess(msg)
                                if (intent.getStringExtra("type")!="account")
//                                    if (!SP.ISYinsi.get()) {
//                                        startActivity<WebNoTokenActivity> {
//                                            putExtra(WebNoTokenActivity.URL, "http://beauty.hebzd.com/static/xieyi/privacy.html")
//                                            putExtra(WebNoTokenActivity.TITLE, "隐私协议")
//                                        }
//                                    } else {
                                        startActivity<MainActivity> { }
//                                    }
                                finish()
                            }
                        })

                }
            }
        }
    }

}